﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class FrmMarkDuctTag : System.Windows.Forms.Form
	{
		public FrmMarkDuctTag()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.tagOperate = new HYTagOperate();
		}

		public FrmMarkDuctTag(bool is3Dview)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.tagOperate = new HYTagOperate();
			this.Is3Dview = is3Dview;
		}

		[DllImport("User32.dll")]
		private static extern int SendMessage(int Handle, int wMsg, int wParam, int lParam);

		public static void SetListViewSpacing(ListView lst, int x, int y)
		{
			FrmMarkDuctTag.SendMessage(lst.Handle.ToInt32(), 4149, 0, x * 65536 + y);
		}

		private void FrmMarkDuctTag_Load(object sender, EventArgs e)
		{
			string text = this.tagOperate.GetNodeValue("DuctTag", "style");
			string nodeValue = this.tagOperate.GetNodeValue("DuctTag", "diameter");
			string nodeValue2 = this.tagOperate.GetNodeValue("DuctTag", "type");
			string nodeValue3 = this.tagOperate.GetNodeValue("DuctTag", "flow");
			string nodeValue4 = this.tagOperate.GetNodeValue("DuctTag", "velocity");
			string nodeValue5 = this.tagOperate.GetNodeValue("DuctTag", "level");
			string nodeValue6 = this.tagOperate.GetNodeValue("DuctTag", "method");
			this.txtMinLength.Text = this.tagOperate.GetNodeValue("DuctTag", "minLength");
			this.txtDisToDuct.Text = this.tagOperate.GetNodeValue("DuctTag", "tagDistance");
			this.distanceToDuct = Convert.ToDouble(this.txtDisToDuct.Text);
			this.autoDis = Convert.ToDouble(this.tagOperate.GetNodeValue("DuctTag", "autoDis"));
			this.offsetStyle = this.tagOperate.GetNodeValue("DuctTag", "offsetstyle");
			this.cmbOffset.SelectedItem = ((this.offsetStyle != "") ? this.offsetStyle : "中部");
			this.ckbDele.Checked = (this.tagOperate.GetNodeValue("DuctTag", "IsDelete") == "Yes");
			this.GetEnabled();
			foreach (string text2 in this.listImageName)
			{
				Image image = Image.FromFile(this.tagOperate.imagePath + text2 + ".png");
				this.imageList1.Images.Add(image);
				this.listView1.LargeImageList.Images.Add(text2, image);
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Tag = text2;
				listViewItem.Text = text2;
				listViewItem.ImageKey = text2;
				this.listView1.Items.Add(listViewItem);
				FrmMarkDuctTag.SetListViewSpacing(this.listView1, 75, 110);
			}
			if (this.Is3Dview)
			{
				this.label3.Visible = true;
				this.label4.Visible = true;
				if (text == "正交引线中" || text == "引线中" || text == "正交引线上" || text == "引线上")
				{
					text = "管线上";
				}
			}
			else
			{
				this.label3.Visible = false;
				this.label4.Visible = false;
			}
			foreach (object obj in this.listView1.Items)
			{
				ListViewItem listViewItem2 = (ListViewItem)obj;
				if (listViewItem2.ImageKey == text)
				{
					listViewItem2.Selected = true;
				}
			}
			if (nodeValue == "Yes")
			{
				this.ckbDiameter.Checked = true;
			}
			if (nodeValue3 == "Yes")
			{
				this.ckbFlow.Checked = true;
			}
			if (nodeValue4 == "Yes")
			{
				this.ckbVelocity.Checked = true;
			}
			if (nodeValue5 == "Yes")
			{
				this.ckbLevel.Checked = true;
			}
			if (nodeValue2 == "Yes")
			{
				this.ckbDuctType.Checked = true;
			}
			if (nodeValue6 == "single")
			{
				this.rbtnSingle.Checked = true;
				return;
			}
			if (!(nodeValue6 == "auto"))
			{
				return;
			}
			this.rbtnAutomatic.Checked = true;
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Dispose();
		}

		private void rbtnAutomatic_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtnAutomatic.Checked)
			{
				this.txtMinLength.ReadOnly = false;
				return;
			}
			this.txtMinLength.ReadOnly = true;
		}

		private void SetButtonTag(Button button)
		{
		}

		private void SetBtnImage(Button button)
		{
		}

		private void btnStyle1_Click(object sender, EventArgs e)
		{
			Button button = sender as Button;
			this.SetButtonTag(button);
			this.SetBtnImage(button);
		}

		private void btnMark_Click(object sender, EventArgs e)
		{
			if (this.rbtnAutomatic.Checked)
			{
				this.markMode = "Auto";
				this.tagOperate.SetNodeValue("DuctTag", "method", "auto");
			}
			else if (this.rbtnSingle.Checked)
			{
				this.markMode = "Man";
				this.tagOperate.SetNodeValue("DuctTag", "method", "single");
			}
			this.markContentList = this.GetTagContent();
			if (this.ckbDiameter.Checked)
			{
				this.tagOperate.SetNodeValue("DuctTag", "diameter", "Yes");
			}
			else
			{
				this.tagOperate.SetNodeValue("DuctTag", "diameter", "No");
			}
			if (this.ckbDuctType.Checked)
			{
				this.tagOperate.SetNodeValue("DuctTag", "type", "Yes");
			}
			else
			{
				this.tagOperate.SetNodeValue("DuctTag", "type", "No");
			}
			if (this.ckbFlow.Checked)
			{
				this.tagOperate.SetNodeValue("DuctTag", "flow", "Yes");
			}
			else
			{
				this.tagOperate.SetNodeValue("DuctTag", "flow", "No");
			}
			if (this.ckbLevel.Checked)
			{
				this.tagOperate.SetNodeValue("DuctTag", "level", "Yes");
			}
			else
			{
				this.tagOperate.SetNodeValue("DuctTag", "level", "No");
			}
			if (this.ckbVelocity.Checked)
			{
				this.tagOperate.SetNodeValue("DuctTag", "velocity", "Yes");
			}
			else
			{
				this.tagOperate.SetNodeValue("DuctTag", "velocity", "No");
			}
			this.offsetStyle = this.cmbOffset.SelectedItem.ToString();
			this.tagOperate.SetNodeValue("DuctTag", "offsetstyle", this.offsetStyle);
			this.tagOperate.SetNodeValue("DuctTag", "style", this.listView1.SelectedItems[0].ImageKey);
			this.markStyle = this.listView1.SelectedItems[0].ImageKey;
			this.minLength = (double)Convert.ToInt32(this.txtMinLength.Text);
			this.distanceToDuct = (double)Convert.ToInt32(this.txtDisToDuct.Text);
			this.tagOperate.SetNodeValue("DuctTag", "minLength", this.txtMinLength.Text);
			this.tagOperate.SetNodeValue("DuctTag", "tagDistance", this.txtDisToDuct.Text);
			if (this.ckbDele.Checked || !this.ckbDele.Enabled)
			{
				this.IsDelete = true;
				this.tagOperate.SetNodeValue("DuctTag", "IsDelete", "Yes");
			}
			else
			{
				this.IsDelete = false;
				this.tagOperate.SetNodeValue("DuctTag", "IsDelete", "No");
			}
			this.tagOperate.SaveXml();
			base.DialogResult = DialogResult.Yes;
		}

		private List<string> GetTagContent()
		{
			List<string> list = new List<string>();
			if (this.ckbDiameter.Checked)
			{
				list.Add("风管尺寸标注");
			}
			if (this.ckbVelocity.Checked)
			{
				list.Add("风管风速标注");
			}
			if (this.ckbFlow.Checked)
			{
				list.Add("风管流量标注");
			}
			if (this.ckbLevel.Checked)
			{
				list.Add("风管偏移量标注");
			}
			if (this.ckbDuctType.Checked)
			{
				list.Add("风管系统缩写标注");
			}
			return list;
		}

		private IndependentTag CreatMarkTag(Autodesk.Revit.DB.Document doc, Autodesk.Revit.DB.View dbview, XYZ pnt, Duct duct)
		{
			TagMode tagMode = 0;
			TagOrientation tagOrientation = 0;
			return RevitVersionFuncs.NewTag(doc, dbview, new Reference(duct), false, tagMode, tagOrientation, pnt);
		}

		private void listView1_SelectedIndexChanged(object sender, EventArgs e)
		{
			foreach (object obj in this.listView1.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Selected)
				{
					if (listViewItem.ImageKey == "管线中")
					{
						this.txtDisToDuct.Enabled = false;
					}
					else
					{
						this.txtDisToDuct.Enabled = true;
					}
					if (listViewItem.ImageKey == "正交引线中" || listViewItem.ImageKey == "引线中")
					{
						this.ckbFlow.Enabled = true;
						this.ckbVelocity.Enabled = true;
					}
					else
					{
						this.ckbFlow.Enabled = false;
						this.ckbVelocity.Enabled = false;
					}
				}
			}
		}

		private void rbtnSingle_CheckedChanged(object sender, EventArgs e)
		{
			this.GetEnabled();
		}

		private void GetEnabled()
		{
			if (this.rbtnSingle.Checked)
			{
				this.groupBox4.Enabled = false;
				this.ckbDele.Enabled = true;
				return;
			}
			this.groupBox4.Enabled = true;
			this.ckbDele.Enabled = false;
		}

		private void ckbDuctType_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckbLevel.Checked)
			{
				this.cmbOffset.Enabled = true;
			}
			else
			{
				this.cmbOffset.Enabled = false;
			}
			if (!this.ckbDiameter.Checked && !this.ckbDuctType.Checked && !this.ckbLevel.Checked)
			{
				this.ckbDuctType.Checked = true;
			}
		}

		private void ckbFlow_CheckedChanged(object sender, EventArgs e)
		{
			if (!this.ckbFlow.Checked && !this.ckbVelocity.Checked)
			{
				this.ckbFlow.Checked = true;
			}
		}

		private void listView1_Validated(object sender, EventArgs e)
		{
			foreach (object obj in this.listView1.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Selected && this.Is3Dview && (listViewItem.ImageKey == "正交引线中" || listViewItem.ImageKey == "引线中" || listViewItem.ImageKey == "正交引线上" || listViewItem.ImageKey == "引线上"))
				{
					YJKMessageBox.Information("三维视图下，“" + listViewItem.ImageKey + "”标注方式不可用！");
					listViewItem.Selected = false;
					this.listView1.Items[0].Selected = true;
				}
			}
		}

		public bool IsDelete;

		public double minLength;

		public double distanceToDuct;

		public string markMode = "";

		public List<string> markContentList = new List<string>();

		public string markStyle = "";

		public double tagDis;

		public double autoDis;

		private HYTagOperate tagOperate;

		public string offsetStyle = "";

		private bool Is3Dview;

		private string[] listImageName = new string[]
		{
			"管线上",
			"管线中",
			"引线上",
			"引线中",
			"正交引线上",
			"正交引线中"
		};

		private const int LVM_FIRST = 4096;

		private const int LVM_SETICONSPACING = 4149;
	}
}
